#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<stdbool.h>
#include <errno.h>
#include <stdint.h>
#include "simple_SQL_compiler.h"
#include "REPL.h"

/*
 * main function: just a infinite loop
 * first ,if start with a . check up is the meta or not
 * second ,check the sql statement right or not
 * third ,execute the sql statement in the virtual machine
 */
int main(int argc, char *argv[]) {
    debug("main",1);
    InputBuffer *input_buffer = new_input_buffer();
    debug("main",2);
    while (true) {
        print_prompt();
        read_input(input_buffer);
        if (input_buffer->buffer[0] == '.') {//judge is the meta command or not like .like
            switch (do_meta_command(input_buffer)) {
                case META_COMMAND_SUCCESS:
                    continue;
                case META_COMMAND_UNRECOGNIZED_COMMAND:
                    printf("Unrecognized command '%s'\n", input_buffer->buffer);
                    continue;
            }
        }

// except the meta command , now analyze the token
        Statement statement;
        switch (prepare_statement(input_buffer, &statement)) {
            case PREPARE_SUCCESS:
                break;
            case PREPARE_UNRECOGNIZED_STATEMENT:
                printf("Unrecognized keyword at start of '%s'.\n", input_buffer->buffer);
                continue;//enter the next loop
        }
        execute_statement(&statement);
        //close_input_buffer(input_buffer);//i don't why add this function will report an error, Illegal exit
        printf("Executed.\n");
    }
}

